﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using xIx.Web.AccessControl;
using System.Collections.Generic;

public partial class SiteLogin : xIx.Web.UI.PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    protected void LogoffButton_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
        Response.Redirect(FormsAuthentication.LoginUrl);
    }

    protected void LoginButton_Click(object sender, EventArgs e)
    {
        Page.Validate("Login");
        if (!Page.IsValid) return;

        string acc = UserName.Text;
        string pwd = Password.Text;

        //DataTable dt = null;
        //using (xIx.Data.DbHelper dh = new xIx.Data.DbHelper())
        //{
        //    dt = dh.Table["Sys_Users"].Where("LOWER(ID)=LOWER(@UID) and Pwd=@UPWD").Select(acc, pwd);
        //}

        //if (dt.Rows.Count == 0)
        //{
        //    Alert("登录错误。"); return;
        //}
        //DataRow dr = dt.Rows[0];
        //BizAccountInfo ai = new BizAccountInfo();
        //ai.ID = dr["ID"].ToString();
        //ai.Name = ai.Title = dr["Title"].ToString();
        //ai.Role = ai.RoleTitle = dr["Role"].ToString();
        //ai.InstituteLevel = int.Parse(dr["Group"].ToString());
        //BizPrincipal.SetAuthCookie(ai);
        ////Session["AccountInfo"] = ai;

        BizAccountInfo ai = BizPrincipal.TryLogin(acc, pwd);
        if (null == ai) {
            Alert("登录错误。"); return;
        }

        BizPrincipal.SetAuthCookie(ai);

        try
        {
            if (!string.IsNullOrEmpty(Request["ReturnUrl"])) Response.Redirect(Request["ReturnUrl"]);
            else Response.Redirect(FormsAuthentication.DefaultUrl, true);
        }
        catch (System.Threading.ThreadAbortException ex)
        {
            //do nothing
        }
    }
}
